package com.qububu.db.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.OrderFormInfoDao;
import com.qububu.emporium.orderform.dto.OrderFormInfoDto;
import com.qububu.emporium.orderform.dto.OrderFormInfoDto.OrderPayType;
import com.qububu.emporium.orderform.dto.OrderFormInfoDto.OrderStatus;
import com.qububu.emporium.shoppingCart.dto.CartSaveGoodsMessageDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

/**
 * @功能 与订单有关的数据库操作
 * @author LH
 * */
@Repository
public class OrderFormInfoDaoImpl implements OrderFormInfoDao{

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * @功能 遍历订单列表
	 * @author LH
	 * @return ArrayList<OrderFormInfoDto> 订单列表信息
	 */
	public List<OrderFormInfoDto> getOrderFormListByOrderStatus(int status) {
		List<OrderFormInfoDto> orderList = new ArrayList<OrderFormInfoDto>();
		String sql = "SELECT * FROM order_form WHERE status=?";
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, status);
		OrderFormInfoDto orderForm = null;
		for (Map<String, Object> elem : list){
			orderForm = new OrderFormInfoDto((Long) elem.get("order_id"), 
					String.valueOf(elem.get("order_no")));
			
			orderForm.setOrderCreateDttm(ParamConverter.convertToInteger(String.valueOf(elem.get("order_create_dttm"))));
			orderForm.setOrderPayType(OrderPayType.getOrderPayType(ParamConverter.convertToInteger(String.valueOf(elem.get("order_pay_type")))));
			orderForm.setOrderReceiveDt(ParamConverter.convertToInteger(String.valueOf(elem.get("order_receive_dt"))));
			orderForm.setOrderAmount(ParamConverter.convertToDouble((String.valueOf(elem.get("order_amount")))));
			orderForm.setRemark(String.valueOf(elem.get("remark")));
			orderForm.setStatus(OrderStatus.getOrderStatus(ParamConverter.convertToInteger(String.valueOf(elem.get("status")))));
			orderForm.setUserAddr(String.valueOf(elem.get("buyer_address")));
			orderForm.setUserName(String.valueOf(elem.get("buyer_name")));
			orderForm.setUserPhoneNumber(String.valueOf(elem.get("buyer_phonenumber")));
			
			orderList.add(orderForm);
		}
		
		return orderList;
	}

	public void doAddOrderForm(OrderFormInfoDto orderForm, List<CartSaveGoodsMessageDto> goodsList) {
		String sql = "insert into order_form(order_no, order_create_dttm, order_pay_type, order_receive_dt, order_amount, buyer_name, buyer_phonenumber, buyer_address, remark, status) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		final String orderNo = orderForm.getOrderNo();
		Object[] orderValues = {
				orderNo,
				orderForm.getOrderCreateDttm(),
				orderForm.getOrderPayType().toIntValue(),
				orderForm.getOrderReceiveDt(),
				orderForm.getOrderAmount(),
				orderForm.getUserName(),
				orderForm.getUserPhoneNumber(),
				orderForm.getUserAddr(),
				orderForm.getRemark(),
				orderForm.getStatus().toIntValue()
		};
		
		jdbcTemplate.update(sql, orderValues);
		String goodsSql = "insert into order_goods(goods_id, goods_quantity, order_no) values(?, ?, ?)";
		final List<CartSaveGoodsMessageDto> insertList = goodsList;
		jdbcTemplate.batchUpdate(goodsSql, new BatchPreparedStatementSetter() {
			
			public void setValues(PreparedStatement ps, int no) throws SQLException {
				CartSaveGoodsMessageDto goods = insertList.get(no);
				int i = 0;
				ps.setInt(++i, ParamConverter.convertToInteger(SystemUtil.decrypt(goods.getGoodsId())));
				ps.setInt(++i, goods.getGoodsQuantity());
				ps.setString(++i, orderNo);
			}
			
			public int getBatchSize() {
				return insertList.size();
			}
		});
	}

	public List<CartSaveGoodsMessageDto> getGoodsListByOrderNo(String orderNo) {
		String sql = "SELECT * FROM order_goods o, goods_info g WHERE o.goods_id=g.goods_id AND o.order_no=?";
		
		List<Map<String, Object>> allGoods = jdbcTemplate.queryForList(sql, orderNo);
		List<CartSaveGoodsMessageDto> goodsList = new ArrayList<CartSaveGoodsMessageDto>();
		CartSaveGoodsMessageDto goods = null;
		for (Map<String, Object> elem : allGoods) {
			goods = new CartSaveGoodsMessageDto(ParamConverter.convertToInteger(String.valueOf(elem.get("goods_id"))));
			goods.setGoodsName(String.valueOf(elem.get("goods_name")));
			goods.setGoodsPicUrl(String.valueOf(elem.get("goods_pic_url")));
			goods.setGoodsPrice(String.valueOf(elem.get("goods_price")));
			goods.setGoodsUseNum(Integer.parseInt(String.valueOf(elem.get("goods_use_num"))));
			goods.setGoodsQuantity(ParamConverter.convertToInteger(String.valueOf(elem.get("goods_quantity"))));
			goodsList.add(goods);
		}
		return goodsList;
	}

	public void doChangeOrderStatus(String orderId, int status) {
		String sql = "UPDATE order_form SET status=? WHERE order_id=?";
		jdbcTemplate.update(sql, new Object[]{status, ParamConverter.convertToInteger(SystemUtil.decrypt(orderId))});
	}



	/**
	 *@功能 删除订单 
	 *@author LH
	 *@param orderId 订单id
	 */
	public boolean doDeleteOrderForm(String orderNo) {
		
		String sql = "delete from order_form where order_no = ?";
		jdbcTemplate.update(sql, orderNo);
		
		return true;
	}

	public List<OrderFormInfoDto> getQueryOrderListById(String userId) {
		// TODO Auto-generated method stub
		List<OrderFormInfoDto> orderList = null;
		orderList = new ArrayList<OrderFormInfoDto>();
		
		String sql = "select * from order_form where user_id = ?";
		
		List<Map<String, Object>> orderMap = jdbcTemplate.queryForList(sql, userId);
		OrderFormInfoDto orderFormInfo = null;
		/*for (Map<String, Object> map : orderMap) {
			orderFormInfo = new OrderFormInfoDto((Long) map.get("order_id"), (Long) map.get("user_shipping_addr_id"), (Long) map.get("goods_info_id"), (Long) map.get("user_id"));
			
			orderFormInfo.setOrderAmount(ParamConverter.convertToInteger(String.valueOf(map.get("order_amount"))));
			orderFormInfo.setOrderCreateDttm(ParamConverter.convertToInteger(String.valueOf(map.get("order_create_dttm"))));
			orderFormInfo.setOrderDeliveryName(String.valueOf( map.get("order_delivery_name")));
			orderFormInfo.setOrderNo(String.valueOf(map.get("order_no")));
			orderFormInfo.setOrderPayType(String.valueOf(map.get("order_pay_type")));
			orderFormInfo.setOrderReceiveDt(String.valueOf(map.get("order_receive_dt")));
			orderFormInfo.setOrderType(ParamConverter.convertToInteger(String.valueOf(map.get("order_type"))));
			
			orderList.add(orderFormInfo);
		}*/
		
		return orderList;
	}

	public OrderFormInfoDto getQueryOrderInfoDetalByOrderNo(String orderNo) {
		// TODO Auto-generated method stub
		String sql = "select * from order_form where order_no = ?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, orderNo);
		/*
		OrderFormInfoDto orderFormInfo = new OrderFormInfoDto((Long) map.get("order_id"), (Long) map.get("user_shipping_addr_id"), (Long) map.get("goods_info_id"), (Long) map.get("user_id"));
		orderFormInfo.setOrderAmount(ParamConverter.convertToInteger(String.valueOf(map.get("order_amount"))));
		orderFormInfo.setOrderCreateDttm(ParamConverter.convertToInteger(String.valueOf(map.get("order_create_dttm"))));
		orderFormInfo.setOrderDeliveryName(String.valueOf( map.get("order_delivery_name")));
		orderFormInfo.setOrderNo(String.valueOf(map.get("order_no")));
		orderFormInfo.setOrderPayType(String.valueOf(map.get("order_pay_type")));
		orderFormInfo.setOrderReceiveDt(String.valueOf(map.get("order_receive_dt")));
		orderFormInfo.setOrderType(ParamConverter.convertToInteger(String.valueOf(map.get("order_type"))));
		
		return orderFormInfo;*/
		return null;
	}
}
